package transport.netty.client;

import entity.RpcResponse;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

//使用一个 类来储存 响应
public class UnprocessedRequests {
    private static ConcurrentHashMap<String, CompletableFuture<RpcResponse>> unprocessedResponseFutures = new ConcurrentHashMap();

    public void put(String requestId, CompletableFuture<RpcResponse> future){
        unprocessedResponseFutures.put(requestId,future);
    }
    public void remove(String requestId){
        unprocessedResponseFutures.remove(requestId);
    }

    //异步返回回调结果
    public void complete(String requestId,RpcResponse rpcResponse){
        CompletableFuture<RpcResponse> future = unprocessedResponseFutures.get(requestId);
        if(future != null){
            future.complete(rpcResponse);
        }else {
            throw new IllegalStateException();
        }
    }
}
